//
//  UIImage+ABCDynamic.m
//  ABCAppearanceKit_Example
//
//  Created by mac on 2022/9/7.
//  Copyright © 2022 Joe. All rights reserved.
//

#import "UIImage+ABCDynamic.h"
#import <objc/runtime.h>

@implementation UIImage (ABCDynamic)

+ (UIImage *)abc_dynamicWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage {
    if (@available(iOS 13.0, *)) {
        if (!darkImage) {
            return lightImage;
        }
        UIImageAsset *imageAsset = [[UIImageAsset alloc]init];
        UITraitCollection *currentTraitCollection = [UITraitCollection currentTraitCollection];
        [imageAsset registerImage:lightImage withTraitCollection:[UITraitCollection traitCollectionWithTraitsFromCollections:@[currentTraitCollection, [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight], [UITraitCollection traitCollectionWithDisplayScale:lightImage.scale]]]];
        
        [imageAsset registerImage:darkImage withTraitCollection:[UITraitCollection traitCollectionWithTraitsFromCollections:@[currentTraitCollection, [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark], [UITraitCollection traitCollectionWithDisplayScale:lightImage.scale]]]];
       
        UIImage *dynamicImage = [imageAsset imageWithTraitCollection:currentTraitCollection];
        return dynamicImage;
    }
    return lightImage;
}

@end
